package com.wufun.overseas.view;

import android.app.Activity;
import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ActivityInfo;
import android.media.AudioManager;
import android.os.Build;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.ProgressBar;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import com.facebook.drawee.view.SimpleDraweeView;
import com.wufun.overseas.R;
import com.wufun.overseas.utils.ToastUtils;
import com.wufun.overseas.view.ptr.PrefUtil;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Timer;
import java.util.TimerTask;
import java.util.logging.Logger;

import fm.jiecao.jcvideoplayer_lib.JCMediaManager;
import fm.jiecao.jcvideoplayer_lib.JCUserAction;
import fm.jiecao.jcvideoplayer_lib.JCUserActionStandard;
import fm.jiecao.jcvideoplayer_lib.JCUtils;
import fm.jiecao.jcvideoplayer_lib.JCVideoPlayer;

import static fm.jiecao.jcvideoplayer_lib.JCUserAction.ON_QUIT_FULLSCREEN;
import static fm.jiecao.jcvideoplayer_lib.JCUserAction.ON_QUIT_TINYSCREEN;


/**
 * Created by xiet on 2017/9/5.
 */

public class StandardVideoView extends JCVideoPlayer {

    protected static Timer DISMISS_CONTROL_VIEW_TIMER;

    public ImageView backButton;
    public ProgressBar bottomProgressBar, loadingProgressBar;
    public TextView titleTextView;
    public SimpleDraweeView thumbImageView;
    public ImageView tinyBackImageView;
    public LinearLayout batteryTimeLayout;
    public ImageView battery_level;
    public TextView video_current_time;
    public TextView retryTextView;
    public TextView clarity;
    public PopupWindow clarityPopWindow;

    protected DismissControlViewTimerTask mDismissControlViewTimerTask;
    protected Dialog mProgressDialog;
    protected ProgressBar mDialogProgressBar;
    protected TextView mDialogSeekTime;
    protected TextView mDialogTotalTime;
    protected ImageView mDialogIcon;
    protected Dialog mVolumeDialog;
    protected ProgressBar mDialogVolumeProgressBar;
    protected TextView mDialogVolumeTextView;
    protected ImageView mDialogVolumeImageView;
    protected Dialog mBrightnessDialog;
    protected ProgressBar mDialogBrightnessProgressBar;
    protected TextView mDialogBrightnessTextView;
    private boolean           brocasting      = false;
    private BroadcastReceiver battertReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
                int level   = intent.getIntExtra("level", 0);
                int scale   = intent.getIntExtra("scale", 100);
                int percent = level * 100 / scale;
                if (percent < 15) {
                    battery_level.setBackgroundResource(R.drawable.jc_battery_level_10);
                } else if (percent >= 15 && percent < 40) {
                    battery_level.setBackgroundResource(R.drawable.jc_battery_level_30);
                } else if (percent >= 40 && percent < 60) {
                    battery_level.setBackgroundResource(R.drawable.jc_battery_level_50);
                } else if (percent >= 60 && percent < 80) {
                    battery_level.setBackgroundResource(R.drawable.jc_battery_level_70);
                } else if (percent >= 80 && percent < 95) {
                    battery_level.setBackgroundResource(R.drawable.jc_battery_level_90);
                } else if (percent >= 95 && percent <= 100) {
                    battery_level.setBackgroundResource(R.drawable.jc_battery_level_100);
                }
                getContext().unregisterReceiver(battertReceiver);
                brocasting = false;
            }
        }
    };

    public StandardVideoView(Context context) {
        super(context);
    }

    public StandardVideoView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public void init(Context context) {
        super.init(context);
        FULLSCREEN_ORIENTATION = ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE;

        batteryTimeLayout = (LinearLayout) findViewById(R.id.battery_time_layout);
        bottomProgressBar = (ProgressBar) findViewById(R.id.bottom_progress);
        titleTextView = (TextView) findViewById(R.id.title);
        backButton = (ImageView) findViewById(R.id.back);
        thumbImageView = (SimpleDraweeView) findViewById(R.id.thumb);
        loadingProgressBar = (ProgressBar) findViewById(R.id.loading);
        tinyBackImageView = (ImageView) findViewById(R.id.back_tiny);
        battery_level = (ImageView) findViewById(R.id.battery_level);
        video_current_time = (TextView) findViewById(R.id.video_current_time);
        retryTextView = (TextView) findViewById(R.id.retry_text);
        clarity = (TextView) findViewById(R.id.clarity);

        if (thumbImageView != null)
            thumbImageView.setOnClickListener(this);
        if (backButton != null)
            backButton.setOnClickListener(this);
        if (tinyBackImageView != null)
            tinyBackImageView.setOnClickListener(this);
        if (clarity != null)
            clarity.setOnClickListener(this);

        initCustomView();
    }

    @Override
    public void setUp(String url, int screen, Object... objects) {
        this.url = url;
        super.setUp(url, screen, objects);
    }

    public void setUp(LinkedHashMap urlMap, int defaultUrlMapIndex, int screen, Object... objects) {
        super.setUp(urlMap, defaultUrlMapIndex, screen, objects);
        if (objects.length == 0) return;
        titleTextView.setText(objects[0].toString());
        if (currentScreen == SCREEN_WINDOW_FULLSCREEN) {
//                this.fullscreenButton.setImageResource(R.drawable.jc_shrink);
            this.fullscreenButton.setImageResource(R.drawable.mediacontroller_fullscreen_out);
            backButton.setVisibility(View.VISIBLE);
            tinyBackImageView.setVisibility(View.INVISIBLE);
            if (null != batteryTimeLayout) batteryTimeLayout.setVisibility(View.VISIBLE);
            if (null != clarity) if (urlMap.size() == 1) {
                clarity.setVisibility(GONE);
            } else {
                clarity.setText(getCurrentUrl());
                clarity.setVisibility(View.VISIBLE);
            }
            changeStartButtonSize((int) getResources().getDimension(R.dimen.jc_start_button_w_h_fullscreen));
        } else if (currentScreen == SCREEN_LAYOUT_NORMAL
                || currentScreen == SCREEN_LAYOUT_LIST) {
//                this.fullscreenButton.setImageResource(R.drawable.jc_enlarge);
            this.fullscreenButton.setImageResource(R.drawable.mediacontroller_fullscreen_in);
            backButton.setVisibility(View.GONE);
            tinyBackImageView.setVisibility(View.INVISIBLE);
            changeStartButtonSize((int) getResources().getDimension(R.dimen.jc_start_button_w_h_normal));
            if (null != batteryTimeLayout) batteryTimeLayout.setVisibility(View.GONE);
            if (null != clarity) clarity.setVisibility(View.GONE);
        } else if (currentScreen == SCREEN_WINDOW_TINY) {
            tinyBackImageView.setVisibility(View.VISIBLE);
            setAllControlsVisible(View.INVISIBLE, View.INVISIBLE, View.INVISIBLE,
                    View.INVISIBLE, View.INVISIBLE, View.INVISIBLE, View.INVISIBLE);
            if (null != batteryTimeLayout) batteryTimeLayout.setVisibility(View.GONE);
            if (null != clarity) clarity.setVisibility(View.GONE);
        }
        setSystemTimeAndBattery();

    }

    public void changeStartButtonSize(int size) {
        ViewGroup.LayoutParams lp = startButton.getLayoutParams();
        lp.height = size;
        lp.width = size;
        lp = loadingProgressBar.getLayoutParams();
        lp.height = size;
        lp.width = size;
    }

    @Override
    public int getLayoutId() {
//        return R.layout.jc_layout_standard;
        return R.layout.standard_video_view;
    }

    @Override
    public void onStateNormal() {
        super.onStateNormal();
        changeUiToNormal();
    }

    @Override
    public void onStatePreparing() {
        super.onStatePreparing();
        changeUiToPreparingShow();
        startDismissControlViewTimer();
    }

    @Override
    public void onStatePreparingChangingUrl(int urlMapIndex, int seekToInAdvance) {
        super.onStatePreparingChangingUrl(urlMapIndex, seekToInAdvance);
        loadingProgressBar.setVisibility(VISIBLE);
        startButton.setVisibility(INVISIBLE);
    }

    @Override
    public void onStatePlaying() {
        super.onStatePlaying();
        changeUiToPlayingShow();
        startDismissControlViewTimer();
        if (muteWhenPlay) {
//           Log.d(TAG +"currentState=" + CURRENT_STATE_PLAYING);
            setMute(true);
            muteWhenPlay = false;
        } else {
//            Logger.log(TAG + ",setUiWitStateAndScreen--> " + "currentState=" + CURRENT_STATE_PLAYING + ", mute=" + mute);
            setMute(mute);
        }
    }

    @Override
    public void onStatePause() {
        super.onStatePause();
        changeUiToPauseShow();
        cancelDismissControlViewTimer();
    }

    @Override
    public void onStatePlaybackBufferingStart() {
        super.onStatePlaybackBufferingStart();
        changeUiToPlayingBufferingShow();
    }

    @Override
    public void onStateError() {
        super.onStateError();
        changeUiToError();
    }

    @Override
    public void onStateAutoComplete() {
        super.onStateAutoComplete();
        changeUiToCompleteClear();
        cancelDismissControlViewTimer();
        bottomProgressBar.setProgress(100);
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        int id = v.getId();
        if (id == R.id.surface_container) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    break;
                case MotionEvent.ACTION_MOVE:
                    break;
                case MotionEvent.ACTION_UP:
                    startDismissControlViewTimer();
                    if (mChangePosition) {
                        int duration = getDuration();
                        int progress = mSeekTimePosition * 100 / (duration == 0 ? 1 : duration);
                        bottomProgressBar.setProgress(progress);
                    }
                    if (!mChangePosition && !mChangeVolume) {
                        onEvent(JCUserActionStandard.ON_CLICK_BLANK);
                        onClickUiToggle();
                    }
                    break;
            }
        } else if (id == R.id.bottom_seek_progress) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    cancelDismissControlViewTimer();
                    break;
                case MotionEvent.ACTION_UP:
                    startDismissControlViewTimer();
                    break;
            }
        }
        return super.onTouch(v, event);
    }

    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.fullscreen) {
            if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT) {
//                IntentUtil.getInstance().goFullScrVideoPlayer(v.getContext(), this.coverUrl, this.url);
                return;
            }
            int type = currentScreen == SCREEN_WINDOW_FULLSCREEN ? ON_QUIT_FULLSCREEN : ON_QUIT_TINYSCREEN;
            if (JC_USER_EVENT != null) {
                JC_USER_EVENT.onEvent(type, this.url, this.currentScreen, this.objects);
            }
        } else if (i == R.id.autoPlay) {
            boolean b = !mAutoPlay.isSelected();
            mAutoPlay.setSelected(b);
            PrefUtil.getInstance(getContext()).setAutoPlayVideoOnWifi(b);
            ToastUtils.getInstance(getContext()).showToastSystem(b ? getResources().getString(R.string.video_view_auto_play_on) : getResources().getString(R.string.video_view_auto_play_off));
        }

        super.onClick(v);
        if (i == R.id.thumb) {
            if (TextUtils.isEmpty(getCurrentUrl())) {
                Toast.makeText(getContext(), getResources().getString(R.string.no_url), Toast.LENGTH_SHORT).show();
                return;
            }
            if (currentState == CURRENT_STATE_NORMAL) {
                if (!getCurrentUrl().startsWith("file") &&
                        !getCurrentUrl().startsWith("/") &&
                        !JCUtils.isWifiConnected(getContext()) && !WIFI_TIP_DIALOG_SHOWED) {
                    showWifiDialog(JCUserActionStandard.ON_CLICK_START_THUMB);
                    return;
                }
                onEvent(JCUserActionStandard.ON_CLICK_START_THUMB);
                startVideo();
            } else if (currentState == CURRENT_STATE_AUTO_COMPLETE) {
                onClickUiToggle();
            }
        } else if (i == R.id.surface_container) {
            startDismissControlViewTimer();
        } else if (i == R.id.back) {
            int type = currentScreen == SCREEN_WINDOW_FULLSCREEN ? ON_QUIT_FULLSCREEN : ON_QUIT_TINYSCREEN;
            if (JC_USER_EVENT != null) {
                JC_USER_EVENT.onEvent(type, this.url, this.currentScreen, this.objects);
            }
            backPress();
        } else if (i == R.id.back_tiny) {
            int type = ON_QUIT_TINYSCREEN;
            if (JC_USER_EVENT != null) {
                JC_USER_EVENT.onEvent(type, this.url, this.currentScreen, this.objects);
            }
            backPress();
        } else if (i == R.id.clarity) {
//            LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//            final LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.jc_layout_clarity, null);
//
//            OnClickListener mQualityListener = new OnClickListener() {
//                public void onClick(View v) {
//                    int index = (int) v.getTag();
//                    onStatePreparingChangingUrl(index, getCurrentPositionWhenPlaying());
//                    clarity.setText(JCUtils.getKeyFromLinkedMap(urlMap, currentUrlMapIndex));
//                    for (int j = 0; j < layout.getChildCount(); j++) {//设置点击之后的颜色
//                        if (j == currentUrlMapIndex) {
//                            ((TextView) layout.getChildAt(j)).setTextColor(Color.parseColor("#fff85959"));
//                        } else {
//                            ((TextView) layout.getChildAt(j)).setTextColor(Color.parseColor("#ffffff"));
//                        }
//                    }
//                    if (clarityPopWindow != null) {
//                        clarityPopWindow.dismiss();
//                    }
//                }
//            };
//
//            for (int j = 0; j < urlMap.size(); j++) {
//                String key = JCUtils.getKeyFromLinkedMap(urlMap, j);
//                TextView clarityItem = (TextView) View.inflate(getContext(), R.layout.jc_layout_clarity_item, null);
//                clarityItem.setText(key);
//                clarityItem.setTag(j);
//                layout.addView(clarityItem, j);
//                clarityItem.setOnClickListener(mQualityListener);
//                if (j == currentUrlMapIndex) {
//                    clarityItem.setTextColor(Color.parseColor("#fff85959"));
//                }
//            }
//
//            clarityPopWindow = new PopupWindow(layout, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, true);
//            clarityPopWindow.setContentView(layout);
//            clarityPopWindow.showAsDropDown(clarity);
//            layout.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
//            clarityPopWindow.update(clarity, -40, 46, Math.round(layout.getMeasuredWidth() * 2), layout.getMeasuredHeight());
        } else if (i == R.id.mute) {
//            Logger.log(TAG + ",onClick-mute--> " + "mute=" + mute);
            setMute(!mute);
        }
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
        super.onStartTrackingTouch(seekBar);
        cancelDismissControlViewTimer();
    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        super.onStopTrackingTouch(seekBar);
        startDismissControlViewTimer();
    }

    public void onClickUiToggle() {
        if (bottomContainer.getVisibility() != View.VISIBLE) {
            setSystemTimeAndBattery();
//            clarity.setText(JCUtils.getKeyFromLinkedMap(urlMap, currentUrlMapIndex));
        }
        if (currentState == CURRENT_STATE_PREPARING) {
            if (bottomContainer.getVisibility() == View.VISIBLE) {
                changeUiToPreparingClear();
            } else {
                changeUiToPreparingShow();
                setSystemTimeAndBattery();
            }
        } else if (currentState == CURRENT_STATE_PLAYING) {
            if (bottomContainer.getVisibility() == View.VISIBLE) {
                changeUiToPlayingClear();
            } else {
                changeUiToPlayingShow();
            }
        } else if (currentState == CURRENT_STATE_PAUSE) {
            if (bottomContainer.getVisibility() == View.VISIBLE) {
                changeUiToPauseClear();
            } else {
                changeUiToPauseShow();
            }
        } else if (currentState == CURRENT_STATE_PLAYING_BUFFERING_START) {
            if (bottomContainer.getVisibility() == View.VISIBLE) {
                changeUiToPlayingBufferingClear();
            } else {
                changeUiToPlayingBufferingShow();
            }
        }
    }

    public void setSystemTimeAndBattery() {
        SimpleDateFormat dateFormater = new SimpleDateFormat("HH:mm");
        Date date         = new Date();
        if (video_current_time != null)
            video_current_time.setText(dateFormater.format(date));
        if (!brocasting) {
            getContext().registerReceiver(
                    battertReceiver,
                    new IntentFilter(Intent.ACTION_BATTERY_CHANGED)
            );
        }
    }

    public void onCLickUiToggleToClear() {
        if (currentState == CURRENT_STATE_PREPARING) {
            if (bottomContainer.getVisibility() == View.VISIBLE) {
                changeUiToPreparingClear();
            } else {
            }
        } else if (currentState == CURRENT_STATE_PLAYING) {
            if (bottomContainer.getVisibility() == View.VISIBLE) {
                changeUiToPlayingClear();
            } else {
            }
        } else if (currentState == CURRENT_STATE_PAUSE) {
            if (bottomContainer.getVisibility() == View.VISIBLE) {
                changeUiToPauseClear();
            } else {
            }
        } else if (currentState == CURRENT_STATE_AUTO_COMPLETE) {
            if (bottomContainer.getVisibility() == View.VISIBLE) {
                changeUiToCompleteClear();
            } else {
            }
        } else if (currentState == CURRENT_STATE_PLAYING_BUFFERING_START) {
            if (bottomContainer.getVisibility() == View.VISIBLE) {
                changeUiToPlayingBufferingClear();
            } else {
            }
        }
    }

    @Override
    public void setProgressAndText(int progress, int position, int duration) {
        super.setProgressAndText(progress, position, duration);
        if (progress != 0) bottomProgressBar.setProgress(progress);
    }

    @Override
    public void setBufferProgress(int bufferProgress) {
        super.setBufferProgress(bufferProgress);
        if (bufferProgress != 0) bottomProgressBar.setSecondaryProgress(bufferProgress);
    }

    @Override
    public void resetProgressAndTime() {
        super.resetProgressAndTime();
        bottomProgressBar.setProgress(0);
        bottomProgressBar.setSecondaryProgress(0);
    }

    //Unified management Ui
    public void changeUiToNormal() {
        switch (currentScreen) {
            case SCREEN_LAYOUT_NORMAL:
            case SCREEN_LAYOUT_LIST:
                setAllControlsVisible(View.VISIBLE, View.INVISIBLE, View.VISIBLE,
                        View.INVISIBLE, View.VISIBLE, View.VISIBLE, View.INVISIBLE);
                updateStartImage();
                break;
            case SCREEN_WINDOW_FULLSCREEN:
                setAllControlsVisible(View.VISIBLE, View.INVISIBLE, View.VISIBLE,
                        View.INVISIBLE, View.VISIBLE, View.VISIBLE, View.INVISIBLE);
                updateStartImage();
                break;
            case SCREEN_WINDOW_TINY:
                break;
        }
    }

    public void changeUiToPreparingShow() {
        switch (currentScreen) {
            case SCREEN_LAYOUT_NORMAL:
            case SCREEN_LAYOUT_LIST:
                setAllControlsVisible(View.VISIBLE, View.INVISIBLE, View.INVISIBLE,
                        View.VISIBLE, View.VISIBLE, View.VISIBLE, View.INVISIBLE);
                break;
            case SCREEN_WINDOW_FULLSCREEN:
                setAllControlsVisible(View.VISIBLE, View.INVISIBLE, View.INVISIBLE,
                        View.VISIBLE, View.VISIBLE, View.VISIBLE, View.INVISIBLE);
                break;
            case SCREEN_WINDOW_TINY:
                break;
        }

    }

    public void changeUiToPreparingClear() {
        switch (currentScreen) {
            case SCREEN_LAYOUT_NORMAL:
            case SCREEN_LAYOUT_LIST:
                setAllControlsVisible(View.VISIBLE, View.INVISIBLE, View.INVISIBLE,
                        View.VISIBLE, View.VISIBLE, View.VISIBLE, View.INVISIBLE);
                break;
            case SCREEN_WINDOW_FULLSCREEN:
                setAllControlsVisible(View.VISIBLE, View.INVISIBLE, View.INVISIBLE,
                        View.VISIBLE, View.VISIBLE, View.VISIBLE, View.INVISIBLE);
                break;
            case SCREEN_WINDOW_TINY:
                break;
        }

    }

    @Override
    public void onVideoRendingStart() {
        super.onVideoRendingStart();
        setAllControlsVisible(View.VISIBLE, View.INVISIBLE, View.INVISIBLE,
                View.INVISIBLE, View.INVISIBLE, View.INVISIBLE, View.VISIBLE);
        startDismissControlViewTimer();
    }

    public void changeUiToPlayingShow() {
        switch (currentScreen) {
            case SCREEN_LAYOUT_NORMAL:
            case SCREEN_LAYOUT_LIST:
                setAllControlsVisible(View.VISIBLE, View.VISIBLE, View.VISIBLE,
                        View.INVISIBLE, View.INVISIBLE, View.INVISIBLE, View.INVISIBLE);
                updateStartImage();
                break;
            case SCREEN_WINDOW_FULLSCREEN:
                setAllControlsVisible(View.VISIBLE, View.VISIBLE, View.VISIBLE,
                        View.INVISIBLE, View.INVISIBLE, View.INVISIBLE, View.INVISIBLE);
                updateStartImage();
                break;
            case SCREEN_WINDOW_TINY:
                break;
        }

    }

    public void changeUiToPlayingClear() {
        switch (currentScreen) {
            case SCREEN_LAYOUT_NORMAL:
            case SCREEN_LAYOUT_LIST:
                setAllControlsVisible(View.INVISIBLE, View.INVISIBLE, View.INVISIBLE,
                        View.INVISIBLE, View.INVISIBLE, View.INVISIBLE, View.VISIBLE);
                break;
            case SCREEN_WINDOW_FULLSCREEN:
                setAllControlsVisible(View.INVISIBLE, View.INVISIBLE, View.INVISIBLE,
                        View.INVISIBLE, View.INVISIBLE, View.INVISIBLE, View.VISIBLE);
                break;
            case SCREEN_WINDOW_TINY:
                break;
        }

    }

    public void changeUiToPauseShow() {
        switch (currentScreen) {
            case SCREEN_LAYOUT_NORMAL:
            case SCREEN_LAYOUT_LIST:
                setAllControlsVisible(View.VISIBLE, View.VISIBLE, View.VISIBLE,
                        View.INVISIBLE, View.INVISIBLE, View.INVISIBLE, View.INVISIBLE);
                updateStartImage();
                break;
            case SCREEN_WINDOW_FULLSCREEN:
                setAllControlsVisible(View.VISIBLE, View.VISIBLE, View.VISIBLE,
                        View.INVISIBLE, View.INVISIBLE, View.INVISIBLE, View.INVISIBLE);
                updateStartImage();
                break;
            case SCREEN_WINDOW_TINY:
                break;
        }

    }

    public void changeUiToPauseClear() {
        switch (currentScreen) {
            case SCREEN_LAYOUT_NORMAL:
            case SCREEN_LAYOUT_LIST:
                setAllControlsVisible(View.INVISIBLE, View.INVISIBLE, View.INVISIBLE,
                        View.INVISIBLE, View.INVISIBLE, View.INVISIBLE, View.INVISIBLE);
                break;
            case SCREEN_WINDOW_FULLSCREEN:
                setAllControlsVisible(View.INVISIBLE, View.INVISIBLE, View.INVISIBLE,
                        View.INVISIBLE, View.INVISIBLE, View.INVISIBLE, View.INVISIBLE);
                break;
            case SCREEN_WINDOW_TINY:
                break;
        }

    }

    public void changeUiToPlayingBufferingShow() {
        switch (currentScreen) {
            case SCREEN_LAYOUT_NORMAL:
            case SCREEN_LAYOUT_LIST:
                setAllControlsVisible(View.VISIBLE, View.VISIBLE, View.INVISIBLE,
                        View.VISIBLE, View.INVISIBLE, View.INVISIBLE, View.INVISIBLE);
                break;
            case SCREEN_WINDOW_FULLSCREEN:
                setAllControlsVisible(View.VISIBLE, View.VISIBLE, View.INVISIBLE,
                        View.VISIBLE, View.INVISIBLE, View.INVISIBLE, View.INVISIBLE);
                break;
            case SCREEN_WINDOW_TINY:
                break;
        }

    }

    public void changeUiToPlayingBufferingClear() {
        switch (currentScreen) {
            case SCREEN_LAYOUT_NORMAL:
            case SCREEN_LAYOUT_LIST:
                setAllControlsVisible(View.INVISIBLE, View.INVISIBLE, View.INVISIBLE,
                        View.VISIBLE, View.INVISIBLE, View.INVISIBLE, View.VISIBLE);
                updateStartImage();
                break;
            case SCREEN_WINDOW_FULLSCREEN:
                setAllControlsVisible(View.INVISIBLE, View.INVISIBLE, View.INVISIBLE,
                        View.VISIBLE, View.INVISIBLE, View.INVISIBLE, View.VISIBLE);
                updateStartImage();
                break;
            case SCREEN_WINDOW_TINY:
                break;
        }

    }

    public void changeUiToCompleteShow() {
        switch (currentScreen) {
            case SCREEN_LAYOUT_NORMAL:
            case SCREEN_LAYOUT_LIST:
                setAllControlsVisible(View.VISIBLE, View.VISIBLE, View.VISIBLE,
                        View.INVISIBLE, View.VISIBLE, View.INVISIBLE, View.INVISIBLE);
                updateStartImage();
                break;
            case SCREEN_WINDOW_FULLSCREEN:
                setAllControlsVisible(View.VISIBLE, View.VISIBLE, View.VISIBLE,
                        View.INVISIBLE, View.VISIBLE, View.INVISIBLE, View.INVISIBLE);
                updateStartImage();
                break;
            case SCREEN_WINDOW_TINY:
                break;
        }

    }

    public void changeUiToCompleteClear() {
        switch (currentScreen) {
            case SCREEN_LAYOUT_NORMAL:
            case SCREEN_LAYOUT_LIST:
                setAllControlsVisible(View.VISIBLE, View.INVISIBLE, View.VISIBLE,
                        View.INVISIBLE, View.VISIBLE, View.INVISIBLE, View.INVISIBLE);
                updateStartImage();
                break;
            case SCREEN_WINDOW_FULLSCREEN:
                setAllControlsVisible(View.VISIBLE, View.INVISIBLE, View.VISIBLE,
                        View.INVISIBLE, View.VISIBLE, View.INVISIBLE, View.INVISIBLE);
                updateStartImage();
                break;
            case SCREEN_WINDOW_TINY:
                break;
        }

    }

    public void changeUiToError() {
        switch (currentScreen) {
            case SCREEN_LAYOUT_NORMAL:
            case SCREEN_LAYOUT_LIST:
                setAllControlsVisible(View.INVISIBLE, View.INVISIBLE, View.VISIBLE,
                        View.INVISIBLE, View.INVISIBLE, View.VISIBLE, View.INVISIBLE);
                updateStartImage();
                break;
            case SCREEN_WINDOW_FULLSCREEN:
                setAllControlsVisible(View.INVISIBLE, View.INVISIBLE, View.VISIBLE,
                        View.INVISIBLE, View.INVISIBLE, View.VISIBLE, View.INVISIBLE);
                updateStartImage();
                break;
            case SCREEN_WINDOW_TINY:
                break;
        }

    }

    public void setAllControlsVisible(int topCon, int bottomCon, int startBtn, int loadingPro,
                                      int thumbImg, int coverImg, int bottomPro) {
        //TODO 这个地方由于前边的各种状态不是太明白，所以暂时只能这样写一下（目前没发现问题），作者可以优化一下
        if (!isVideoRendingStart && currentScreen != SCREEN_WINDOW_FULLSCREEN && currentScreen != SCREEN_WINDOW_TINY) {
            //只要没开始播放，一直显示缩略图
            thumbImg = VISIBLE;
        }

        topContainer.setVisibility(topCon);
        bottomContainer.setVisibility(bottomCon);
        startButton.setVisibility(startBtn);
        loadingProgressBar.setVisibility(loadingPro);
        thumbImageView.setVisibility(thumbImg);
        bottomProgressBar.setVisibility(bottomPro);

        onControlViewVisibility(bottomCon);
    }

    public void updateStartImage() {
        if (currentState == CURRENT_STATE_PLAYING) {
            startButton.setImageResource(R.drawable.jc_click_pause_selector);
            if (retryTextView != null) retryTextView.setVisibility(INVISIBLE);
        } else if (currentState == CURRENT_STATE_ERROR) {
            startButton.setImageResource(R.drawable.jc_click_error_selector);
            if (retryTextView != null) retryTextView.setVisibility(INVISIBLE);
        } else if (currentState == CURRENT_STATE_AUTO_COMPLETE) {
            startButton.setImageResource(R.drawable.jc_click_replay_selector);
            if (retryTextView != null) retryTextView.setVisibility(VISIBLE);
        } else {
            startButton.setImageResource(R.drawable.jc_click_play_selector);
            if (retryTextView != null) retryTextView.setVisibility(INVISIBLE);
        }
    }

    @Override
    public void showProgressDialog(float deltaX, String seekTime, int seekTimePosition, String totalTime, int totalTimeDuration) {
        super.showProgressDialog(deltaX, seekTime, seekTimePosition, totalTime, totalTimeDuration);
        if (mProgressDialog == null) {
            View localView = LayoutInflater.from(getContext()).inflate(R.layout.jc_dialog_progress, null);
            mDialogProgressBar = ((ProgressBar) localView.findViewById(R.id.duration_progressbar));
            mDialogSeekTime = ((TextView) localView.findViewById(R.id.tv_current));
            mDialogTotalTime = ((TextView) localView.findViewById(R.id.tv_duration));
            mDialogIcon = ((ImageView) localView.findViewById(R.id.duration_image_tip));
            mProgressDialog = createDialogWithView(localView);
        }
        if (!mProgressDialog.isShowing()) {
            mProgressDialog.show();
        }

        mDialogSeekTime.setText(seekTime);
        mDialogTotalTime.setText(" / " + totalTime);
        mDialogProgressBar.setProgress(totalTimeDuration <= 0 ? 0 : (seekTimePosition * 100 / totalTimeDuration));
        if (deltaX > 0) {
            mDialogIcon.setBackgroundResource(R.drawable.jc_forward_icon);
        } else {
            mDialogIcon.setBackgroundResource(R.drawable.jc_backward_icon);
        }
        onCLickUiToggleToClear();
    }

    @Override
    public void dismissProgressDialog() {
        super.dismissProgressDialog();
        if (mProgressDialog != null) {
            mProgressDialog.dismiss();
        }
    }

    @Override
    public void showVolumeDialog(float deltaY, int volumePercent) {
        super.showVolumeDialog(deltaY, volumePercent);
        if (mVolumeDialog == null) {
            View localView = LayoutInflater.from(getContext()).inflate(R.layout.jc_dialog_volume, null);
            mDialogVolumeImageView = ((ImageView) localView.findViewById(R.id.volume_image_tip));
            mDialogVolumeTextView = ((TextView) localView.findViewById(R.id.tv_volume));
            mDialogVolumeProgressBar = ((ProgressBar) localView.findViewById(R.id.volume_progressbar));
            mVolumeDialog = createDialogWithView(localView);
        }
        if (!mVolumeDialog.isShowing()) {
            mVolumeDialog.show();
        }
        if (volumePercent <= 0) {
            mDialogVolumeImageView.setBackgroundResource(R.drawable.jc_close_volume);
        } else {
            mDialogVolumeImageView.setBackgroundResource(R.drawable.jc_add_volume);
        }
        if (volumePercent > 100) {
            volumePercent = 100;
        } else if (volumePercent < 0) {
            volumePercent = 0;
        }
        mDialogVolumeTextView.setText(volumePercent + "%");
        mDialogVolumeProgressBar.setProgress(volumePercent);
        onCLickUiToggleToClear();
    }

    @Override
    public void dismissVolumeDialog() {
        super.dismissVolumeDialog();
        if (mVolumeDialog != null) {
            mVolumeDialog.dismiss();
        }
    }

    @Override
    public void showBrightnessDialog(int brightnessPercent) {
        super.showBrightnessDialog(brightnessPercent);
        if (mBrightnessDialog == null) {
            View localView = LayoutInflater.from(getContext()).inflate(R.layout.jc_dialog_brightness, null);
            mDialogBrightnessTextView = ((TextView) localView.findViewById(R.id.tv_brightness));
            mDialogBrightnessProgressBar = ((ProgressBar) localView.findViewById(R.id.brightness_progressbar));
            mBrightnessDialog = createDialogWithView(localView);
        }
        if (!mBrightnessDialog.isShowing()) {
            mBrightnessDialog.show();
        }
        if (brightnessPercent > 100) {
            brightnessPercent = 100;
        } else if (brightnessPercent < 0) {
            brightnessPercent = 0;
        }
        mDialogBrightnessTextView.setText(brightnessPercent + "%");
        mDialogBrightnessProgressBar.setProgress(brightnessPercent);
        onCLickUiToggleToClear();
    }

    @Override
    public void dismissBrightnessDialog() {
        super.dismissBrightnessDialog();
        if (mBrightnessDialog != null) {
            mBrightnessDialog.dismiss();
        }
    }

    public Dialog createDialogWithView(View localView) {
        Dialog dialog = new Dialog(getContext(), R.style.jc_style_dialog_progress);
        dialog.setContentView(localView);
        Window window = dialog.getWindow();
        window.addFlags(Window.FEATURE_ACTION_BAR);
        window.addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
        window.addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
        window.setLayout(-2, -2);
        WindowManager.LayoutParams localLayoutParams = window.getAttributes();
        localLayoutParams.gravity = Gravity.CENTER;
        window.setAttributes(localLayoutParams);
        return dialog;
    }

    public void startDismissControlViewTimer() {
        cancelDismissControlViewTimer();
        DISMISS_CONTROL_VIEW_TIMER = new Timer();
        mDismissControlViewTimerTask = new DismissControlViewTimerTask();
        DISMISS_CONTROL_VIEW_TIMER.schedule(mDismissControlViewTimerTask, 2500);
    }

    public void cancelDismissControlViewTimer() {
        if (DISMISS_CONTROL_VIEW_TIMER != null) {
            DISMISS_CONTROL_VIEW_TIMER.cancel();
        }
        if (mDismissControlViewTimerTask != null) {
            mDismissControlViewTimerTask.cancel();
        }

    }

    @Override
    public void onAutoCompletion() {
        super.onAutoCompletion();
        cancelDismissControlViewTimer();
    }

    @Override
    public void onCompletion() {
        super.onCompletion();
        cancelDismissControlViewTimer();
        if (clarityPopWindow != null) {
            clarityPopWindow.dismiss();
        }
    }

    public class DismissControlViewTimerTask extends TimerTask {

        @Override
        public void run() {
            if (currentState != CURRENT_STATE_NORMAL
                    && currentState != CURRENT_STATE_ERROR
                    && currentState != CURRENT_STATE_AUTO_COMPLETE) {
                if (getContext() != null && getContext() instanceof Activity) {
                    ((Activity) getContext()).runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            bottomContainer.setVisibility(View.INVISIBLE);
                            topContainer.setVisibility(View.INVISIBLE);
                            startButton.setVisibility(View.INVISIBLE);
                            if (clarityPopWindow != null) {
                                clarityPopWindow.dismiss();
                            }
                            if (currentScreen != SCREEN_WINDOW_TINY) {
                                bottomProgressBar.setVisibility(View.VISIBLE);
                            }

                            onControlViewVisibility(INVISIBLE);
                        }
                    });
                }
            }
        }
    }

    //region 静音、自动播放
    public ImageView muteImageView;
    protected ImageView mAutoPlay;

    protected      boolean noneNetFinishActivity = false;
    public static  boolean mute                  = true;
    private        boolean muteWhenPlay          = false;
    private static boolean muteFullScrBak        = true;//全屏下备份

    public void setNoneNetFinishActivity(boolean noneNetFinishActivity) {
        this.noneNetFinishActivity = noneNetFinishActivity;
    }

    void initCustomView() {
        this.muteImageView = (ImageView) this.findViewById(R.id.mute);
        this.mAutoPlay = (ImageView) this.findViewById(R.id.autoPlay);
        this.muteImageView.setOnClickListener(this);
        this.mAutoPlay.setOnClickListener(this);

        setMuteView(mute);
        this.mAutoPlay.setSelected(PrefUtil.getInstance(getContext()).getAutoPlayVideoOnWifi());
    }

    public void setMuteView(boolean mute) {
        if (mute) {
            this.muteImageView.setImageResource(R.drawable.mute_on);
        } else {
            this.muteImageView.setImageResource(R.drawable.mute_off);
        }
    }

    @Override
    public void onEvent(int type) {
        super.onEvent(type);
        switch (type) {
            case JCUserAction.ON_CLICK_RESUME:
                if (muteWhenPlay) {
                    Logger.getLogger(TAG + ",onEvent-ON_CLICK_RESUME--> " + "mute=" + mute);
                    setMute(true);
                    muteWhenPlay = false;
                }
                break;
            case JCUserAction.ON_QUIT_FULLSCREEN:
            case JCUserAction.ON_QUIT_TINYSCREEN:
                Logger.getLogger(TAG + ",onEvent-ON_QUIT_FULLSCREEN--> " + "mute=" + mute);
                setMute(muteFullScrBak);
                break;
            case JCUserAction.ON_ENTER_FULLSCREEN:
                Logger.getLogger(TAG + ",onEvent-ON_ENTER_FULLSCREEN--> " + "mute=" + mute);
                muteFullScrBak = mute;
                setMute(false);
                break;
        }
    }

    public void setMuteWhenPlay(boolean muteWhenPlay) {
        this.muteWhenPlay = muteWhenPlay;
//        JCVideoPlayer videoPlayer = JCVideoPlayerManager.getCurrentJcvd();
//        if (null != videoPlayer && videoPlayer instanceof StandardVideoView) {
//            ((StandardVideoView) videoPlayer).setMuteWhenPlay(true);
//        }
//        videoPlayer = JCVideoPlayerManager.getFirstFloor();
//        if (null != videoPlayer && videoPlayer instanceof StandardVideoView) {
//            ((StandardVideoView) videoPlayer).setMuteWhenPlay(true);
//        }
//        videoPlayer = JCVideoPlayerManager.getSecondFloor();
//        if (null != videoPlayer && videoPlayer instanceof StandardVideoView) {
//            ((StandardVideoView) videoPlayer).setMuteWhenPlay(true);
//        }
    }

    public void setMute(boolean mute) {
        Logger.getLogger(TAG + ",setMute--> " + "mute=" + mute);
        setMuteOnly(mute);
        StandardVideoView.mute = mute;
    }

    public void setMuteOnly(boolean mute) {
        Logger.getLogger(TAG + ",setMuteOnly--> " + "mute=" + mute);
        try {
            JCMediaManager.instance().mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            if (mute) {
                JCMediaManager.instance().mediaPlayer.setVolume(0.f, 0.f);
            } else {
                JCMediaManager.instance().mediaPlayer.setVolume(1.f, 1.f);
            }
            setMuteView(mute);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //endregion

    //region 网络对话框

//    @Override
//    public void showWifiDialog(int action) {
//        super.showWifiDialog(action);
//        AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
//        builder.setMessage(getResources().getString(R.string.tips_not_wifi));
//        builder.setPositiveButton(getResources().getString(R.string.tips_not_wifi_confirm), new DialogInterface.OnClickListener() {
//            @Override
//            public void onClick(DialogInterface dialog, int which) {
//                dialog.dismiss();
//                onEvent(JCUserActionStandard.ON_CLICK_START_THUMB);
//                startVideo();
//                WIFI_TIP_DIALOG_SHOWED = true;
//            }
//        });
//        builder.setNegativeButton(getResources().getString(R.string.tips_not_wifi_cancel), new DialogInterface.OnClickListener() {
//            @Override
//            public void onClick(DialogInterface dialog, int which) {
//                dialog.dismiss();
//                if (currentScreen == SCREEN_WINDOW_FULLSCREEN) {
//                    dialog.dismiss();
//                    clearFullscreenLayout();
//                }
//            }
//        });
//        builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
//            @Override
//            public void onCancel(DialogInterface dialog) {
//                dialog.dismiss();
//                if (currentScreen == SCREEN_WINDOW_FULLSCREEN) {
//                    dialog.dismiss();
//                    clearFullscreenLayout();
//                }
//            }
//        });
//        builder.create().show();
//    }

//    MyDialog mDialog;

//    public void showWifiDialog(int event) {
//        if (currentState == JCVideoPlayer.CURRENT_STATE_PLAYING) {
//            this.onEvent(JCVideoPlayer.CURRENT_STATE_PLAYING);
//            JCMediaManager.instance().mediaPlayer.pause();
//
//            onStatePause();
//        }
//
//        if (mDialog == null) {
//            mDialog = new MyDialog(getContext(), R.style.MyDialog);
//            mDialog.setContentView(R.layout.bind_dialog);
//        }
//
//        Button dialog_button_cancel = (Button) mDialog.findViewById(R.id.dialog_button_cancel);
//        Button dialog_button_ok     = (Button) mDialog.findViewById(R.id.dialog_button_ok);
//        TextView tip_title            = (TextView) mDialog.findViewById(R.id.tip_title);
//        View divider              = mDialog.findViewById(R.id.divider);
//        TextView dialog_content       = (TextView) mDialog.findViewById(R.id.dialog_content);
//        TextView dialog_desci         = (TextView) mDialog.findViewById(R.id.dialog_desci);
//
//        String submit = this.getResources().getString(R.string.tips_not_wifi_confirm);
//        String cancel = this.getResources().getString(R.string.tips_not_wifi_cancel);
//        String tip    = this.getResources().getString(R.string.tips_not_wifi);
//        submit = "继续观看";
//        cancel = "取消";
//        tip = "您已切换到数据网络，是否继续观看视频？";
//
//
//        Context context  = getContext();
//        final int netState = NetUtil.getNetWorkState(context);
//        if (netState == NetUtil.NETWORK_MOBILE) {
//            divider.setVisibility(VISIBLE);
//            dialog_button_cancel.setVisibility(VISIBLE);
//        } else if (netState == NetUtil.NETWORK_WIFI) {
//        } else {
//            divider.setVisibility(GONE);
//            dialog_button_cancel.setVisibility(GONE);
//            tip = getResources().getString(R.string.network_conn_failed);
//            submit = "关闭";
//        }
//
//        tip_title.setText("网络提示");
//        dialog_content.setText(tip);
//        dialog_desci.setVisibility(View.GONE);
//        dialog_button_ok.setText(submit);
//        dialog_button_cancel.setText(cancel);
//
//        dialog_button_ok.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                if (netState == NetUtil.NETWORK_NONE) {
//                    if (getContext() instanceof Activity
//                            && noneNetFinishActivity) {
//                        ((Activity) getContext()).finish();
//                    }
//                } else {
//                    startVideo();
//                }
//                mDialog.dismiss();
////                JCVideoPlayer.WIFI_TIP_DIALOG_SHOWED = true;
//            }
//        });
//        dialog_button_cancel.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                if (netState == NetUtil.NETWORK_MOBILE) {
//                    if (getContext() instanceof Activity
//                            && noneNetFinishActivity) {
//                        ((Activity) getContext()).finish();
//                    }
//                }
//                mDialog.dismiss();
//            }
//        });
//        if (mDialog != null && !mDialog.isShowing())
//            mDialog.show();
//    }
    //endregion

    private final int ON_CONTROLS_VISIBILITY = 13;

    public void onControlViewVisibility(int visibility) {
        if (JC_USER_EVENT != null && this.isCurrentJcvd()) {
            JC_USER_EVENT.onEvent(ON_CONTROLS_VISIBILITY, this.url, this.currentScreen, visibility);
        }
    }

    public String url = "";

    public String getCurrentUrl() {
        String ret = "";
        try {
//            LinkedHashMap<String, String> map;
//            int index = 0;
//            if (isCurrentJcvd()){
//                map = JCVideoPlayerManager.getSecondFloor().
//            }
//            ret = JCUtils.getCurrentUrlFromMap(urlMap, currentUrlMapIndex);

            ret = this.url;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ret;
    }
}